package vn.fithou.hou_elearning.activity;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import vn.fithou.hou_elearning.R;
import vn.fithou.hou_elearning.adapter.KhoahocAdapter;
import vn.fithou.hou_elearning.parser.XMLParser;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class DanhsachKhoahocActivity extends Activity {
	// All static variables
	static final String URL = "http://10.0.2.2/hou-elearning/Service/HOULMSService.svc/KhoahocGetAllXML";
	// XML node keys
	public static final String KEY_KHOAHOC = "KhoahocEntity"; // nút cha
	public static final String KEY_ID = "PK_iKhoahocID";
	public static final String KEY_NAME = "sTenkhoahoc";
	public static final String KEY_ALIAS = "sKyhieu";
	public static final String KEY_TITLE = "sGhichu";
	public static final String KEY_COUNT = "iSoluongdukien";
	public static final String KEY_THUMB_URL = "sAnhminhhoa";
	
	public static final int REQUEST_CODE_LESSON = 1;
	
	ListView list;
    KhoahocAdapter adapter;
    ArrayList<HashMap<String, String>> courseList = null; //Danh sách khóa học
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);

		courseList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL, "GET"); // getting XML from URL
		Document doc = parser.getDomElement(xml); // getting DOM element
		
		NodeList nl = doc.getElementsByTagName(KEY_KHOAHOC);
		// looping through all song nodes <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// adding each child node to HashMap key => value
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
			map.put(KEY_ALIAS, parser.getValue(e, KEY_ALIAS));
			map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
			map.put(KEY_COUNT, parser.getValue(e, KEY_COUNT));
			map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

			// adding HashList to ArrayList
			courseList.add(map);
		}
		

		list = (ListView)findViewById(R.id.list);
		
		// Getting adapter by passing xml data ArrayList
        adapter = new KhoahocAdapter(this, courseList);        
        list.setAdapter(adapter); //Đặt adapter của list view
        

        /*
		 * Khi click vào khóa học -> lấy ID -> chuyển sang hiển thị danh sách bài học tương ứng
		 * */
        list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int PK_iKhoahocID = Integer.parseInt(courseList.get(position).get(KEY_ID));
				hienthiDanhsachBaihoc(PK_iKhoahocID);
			}
		});	
	}
	/*
	 * Hiển thị danh sách bài học theo ID
	 * */
	public void hienthiDanhsachBaihoc(int PK_iKhoahocID)
    {
        Intent inDsBaihoc = new Intent(DanhsachKhoahocActivity.this, DanhsachBaihocActivity.class);
        Bundle b = new Bundle();
        b.putInt("FK_iKhoahocID", PK_iKhoahocID);
        inDsBaihoc.putExtras(b);
        startActivityForResult(inDsBaihoc, REQUEST_CODE_LESSON);
    }
}